package cn.edu.sdjzu.xg.kcsj.service;

import cn.edu.sdjzu.xg.kcsj.dao.TeacherDao;
import cn.edu.sdjzu.xg.kcsj.domain.Teacher;
import cn.edu.sdjzu.xg.kcsj.domain.User;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class TeacherService {
    private static final TeacherDao teacherDao = TeacherDao.getInstance();
    private static final TeacherService teacherService = new TeacherService();

    private TeacherService() {
    }

    public static TeacherService getInstance() {
        return teacherService;
    }

    //无条件查询全部方法
    public Collection<Teacher> findAll() throws SQLException {
        return this.findAll(null, null);
    }
    //条件查询加分页查询的查询全部
    public Collection<Teacher> findAll(Pagination pagination
            , List<Condition> conditionList_json_str) throws SQLException {
        //获取连接
        Connection conn = JdbcHelper.getConn();
        Collection<Teacher> teachers=
                teacherDao.findAll(pagination,conditionList_json_str,conn);
        //关闭连接
        JdbcHelper.close(conn);
        return teachers;
    }
    //根据id查询
    public Teacher find(Integer id) throws SQLException {
        //获取连接
        Connection conn = JdbcHelper.getConn();
        Teacher teacher = teacherDao.find(id,conn);
        //关闭连接
        JdbcHelper.close(conn);
        return teacher;
    }
    //修改方法
    public boolean update(Teacher teacher) throws SQLException {
        //获取连接
        Connection conn = JdbcHelper.getConn();
        //将自动提交设置为false
        conn.setAutoCommit(false);
        boolean updated = false;
        try {
            //执行修改方法
            updated = teacherDao.update(teacher,conn);
            //提交事务
            conn.commit();
        }catch (SQLException e){
            //回滚到事务开始
            conn.rollback();
            //打印异常
//            log.trace(e.toString());
        }finally {
            //将自动提交设置为true
            conn.setAutoCommit(true);
            //关闭连接
            JdbcHelper.close(conn);
        }
        return updated;
    }
    public boolean deletes(List<Teacher> listToDelete) throws SQLException{
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean deleted =false;
        try {
            for(Teacher e:listToDelete) {
                int id= e.getId();
                //删除用户
                deleted = teacherDao.delete(id, connection);
            }
            //提交事务
            connection.commit();
        }catch (Exception e){
            //回滚
            connection.rollback();
        }finally {
            //将自动提交设为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(connection);
        }
        return deleted ;
    }
    //删除方法
    public boolean delete(Integer id) throws SQLException {
        //如果该教师有关联的课题，不能删除
        //获取连接
        Connection conn = JdbcHelper.getConn();
        //将自动提交设置为false
        conn.setAutoCommit(false);
        boolean deleted = false;

        try {
            //执行删除方法
            deleted = teacherDao.delete(id,conn);
            //提交事务
            conn.commit();
        }catch (SQLException e){
            //回滚到事务开始
            conn.rollback();
            //打印异常
//            log.trace(e.toString());
        }finally {
            //将自动提交设置为true
            conn.setAutoCommit(true);
            //关闭连接
            JdbcHelper.close(conn);
        }
        return deleted;
    }

    /**
     * 添加Teacher对象
     * @param teacher 要添加的Teacher对象
     * @return 添加成功返回true，否则false
     * @throws SQLException
     */
    public Boolean add(Teacher teacher) throws SQLException {
        //获得进行事务的连接，添加教师和添加用户都使用本连接对象
        Connection connection = JdbcHelper.getConn();
        //将自动提交设为false,开始事务
        connection.setAutoCommit(false);
        Boolean added = false;
        try {
            teacher.setId(ActorService.getInstance().add());
            //创建用户对象，密码和用户名取自teacher字段值no
            User user = new User(teacher.getNo(), teacher.getNo(), null, teacher);
            Integer id = UserService.getInstance().add(user);
            //为student的id赋值
            teacher.setId(id);

            added=teacherDao.add(teacher, connection);
            //提交事务
            connection.commit();
        } catch (SQLException e) {
            //回滚事务中所有操作
            e.printStackTrace();
            connection.rollback();
        } finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            JdbcHelper.close(null, connection);
        }
        return added;
    }
//    public int countAll(Connection connection,Department department)throws SQLException{
//        connection.setAutoCommit(false);
//        int count = 0;
//        try {
//            count = teacherDao.countAll(department,connection);
//        }catch (SQLException e){
//            connection.rollback();
//        }finally {
//            connection.setAutoCommit(true);
//        }
//        return count;
//    }

}
